<template>
    <div>
        <el-tag
          v-for="(tag, index) in $store.state.tabList"
          :key="index"
          @click="handleJump(tag)"
          @close="handleClose(tag, index)"
          :closable = "tag.path !== '/index'"
          :type="(tag.path == defaultUrl) ? 'primary' : 'info'"
          style="margin-left: 8px !important;">
          {{tag.name}}
        </el-tag>
    </div>
</template>

<script>
      export default {
        data() {
          return {
            defaultUrl: '/index'
          };
        },
        watch:{
            $route(to) {
                this.defaultUrl = to.path;
            }
        },
        mounted() {
            this.defaultUrl = this.$store.state.menuDefault.path !== '' ? this.$store.state.menuDefault.path : "/index";
        },
        methods: {
            handleJump(val) {
                if (this.$route.path == val.path)
                    return;
                this.$store.dispatch('ADD_MENU', {index: val.index, path: val.path});
                this.$router.push(val.path);
            },
            handleClose(val, index) {
                this.$store.dispatch('DELETE_TAGS', index);
                if (this.$route.path !== val.path) {
                    return;
                } else {
                    //是否是最后一个
                    if (index == this.$store.state.tabList.length) {
                        this.$store.dispatch('ADD_MENU', {index: this.$store.state.tabList[index - 1].index, path: this.$store.state.tabList[index - 1].path});
                        this.$router.push(this.$store.state.tabList[index - 1].path);
                    } else {
                        this.$store.dispatch('ADD_MENU', {index: this.$store.state.tabList[index].index, path: this.$store.state.tabList[index].path});
                        this.$router.push(this.$store.state.tabList[index].path);
                    }
                }
            }
        }
      }
</script>

<style>
</style>
